package service.servlet;

import dao.UserDao;
import domain.User;
import impl.UserDaoImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/loginservlet")
/*servlet 完成业务逻辑 */
/*dao 完成数据库的增删改查 */
public class LoginSerlvet  extends HttpServlet {
    UserDao userDao = new UserDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*1.获取前端页面提交过来的数据*/
        String username = req.getParameter("username");
        String password = req.getParameter("password");
     /*将前端页面获取过来的数据进行封装  封装到一个javabean*/
User loginUser = new User(username,password);
/*完成数据库的查询*/
        try {
            boolean f = userDao.addUser(loginUser);
            /*集合中有数据说明 用户名密码是对的  跳转到 SuccessSerclet
            * 否则跳转到FailServler
            * 根据现有逻辑 如果找到list集合只有一条数据
            *
            * */
            if(f) {
                /*开始跳转到SuccessServlet*/
                req.getRequestDispatcher("/success").forward(req,resp);
            }else{
                req.getRequestDispatcher("/fail").forward(req,resp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
}
